<?php

require_once ROOT_APPLICATION_MODULES_ADMIN_FORMS . DS . "Tag.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_OBJECTS . DS . "Tag.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS . DS . "Tags.php";

require_once ROOT_APPLICATION_MODULES_ADMIN_OBJECTS . DS . "TagItemAssoc.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS . DS . "TagItemAssocs.php";

require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS.DS."Users.php";

require_once '../library/Utility/Converter.php';

class Admin_TagController extends Zend_Controller_Action {

    ///////////////////////
    // V A R I A B L E S //
    ///////////////////////
    private $modelTag;
    private $modelTagItemAssoc;
    private $modelUser;
    private $currentUser;
    
    /////////////////////
    // O V E R I D E S //
    /////////////////////
    public function init() {
        if (Zend_Auth::getInstance()->getIdentity() == FALSE) {
            $this->_redirect('admin/auth/login');
        }
        $layoutPath = APPLICATION_PATH . '/templates/admin/default';
        $option = array('layout' => 'index',
            'layoutPath' => $layoutPath);

        Zend_Layout::startMvc($option);
        
        //init variable
        //MODEL
        $this->modelTag = new Application_Models_Tags();
        $this->modelTagItemAssoc = new Application_Model_Tag_Item_Assocs();
        $this->modelUser = new End_Models_Users();
        
        //OTHER 
        $this->currentUser = $this->getInforUser();
        
        //VIEW 
        $this->view->currentUser = $this->currentUser; 
        /* Initialize action controller here */
    }

    ///////////////////
    // U T I L I E S //
    ///////////////////
    private function getInforUser() {
        try {
            $currentUser = Zend_Auth::getInstance()->getIdentity();
            return $currentUser;
        } catch (Zend_Exception $ex) {
            
        }
    }

    
    ////////////////////
    // A C T I O N S  //
    ////////////////////  
    public function indexAction() {
        $tags = $this->modelTag->fetchAll();
        $this->view->tags = $tags;
    }

    public function addAction() {
        $form = new Application_Forms_Tag();
        $form->submit->setLabel('Save');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                /* Input value */

                $tag_id = $form->getValue('tag_id');
                $tag_text = $form->getValue('tag_text');

                /* Create new tag model */

                $tag = new Tag(
                                $tag_id,
                                $tag_text);

                $inserted_id = $this->modelTag->_insert($tag);

                // Redirect after insert.
                $param = array("tag_id" => $inserted_id);
                $this->_helper->redirector("edit", "tag", "admin", $param);
            } else {
                $form->populate($formData);
            }
        }
    }

    public function editAction() {
        $form = new Application_Forms_Tag();
        $form->submit->setLabel('Save');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                /* Input value */

                $tag_id = $form->getValue('tag_id');
                $tag_text = $form->getValue('tag_text');

                /* Create new tag model */

                $tag = new Tag(
                                $tag_id,
                                $tag_text);

                $inserted_id = $this->modelTag->_update($tag);
            } else {
                $form->populate($formData);
            }
        } else {
            $tag_id = $this->_getParam('tag_id');
            if ($tag_id > 0) {
                $tagArray = Utility_Converter::objectToArray($this->modelTag->_find($tag_id));
                $form->populate($tagArray);
            }
        }
    }

    public function deleteAction() {
        $tag_id = $this->_getParam('tag_id');

        /* Delete associate tag */
        $this->modelTagItemAssoc->DeleteTags($tag_id);

        /* Delete tag */
        $this->modelTag->_delete($tag_id);

        $this->_helper->redirector('index');
    }

}